package com.lujianing.controller;

import com.lujianing.cmd.Abs;
import com.lujianing.cmd.AbsModel;
import com.lujianing.cmd.ChartsData;
import com.lujianing.cmd.CmdConsolePrintVo;
import com.lujianing.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class HelloController {
    private static final Logger logger= LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private Abs abs;
    @RequestMapping("hello")
    public String hello() {
        return "hello world";
    }

    @RequestMapping("hello2")
    @ResponseBody
    public CmdConsolePrintVo hello2() {
//        abs.
        String requests="100";
        String concurrency="3";
        String url="https://www.baidu.com/";
        CmdConsolePrintVo cmdConsolePrintVo=abs.testWayByAbs(requests,concurrency,url);
//        abs.

        return cmdConsolePrintVo;
    }

    @RequestMapping("hello3")
    @ResponseBody
    public ResponseData hello3() {
//        abs.
        String requests="300";
        String concurrency="10";
        String url="https://www.baidu.com/";
//        CmdConsolePrintVo cmdConsolePrintVo=abs.testWayByAbs(requests,concurrency,url);
//        abs.

//        String[] counts=new String[] {"500","1000","1500","2000"};
//        String[] counts=new String[] {"500","1000"};
//        String[] counts=new String[] {"100","500"};
//        String[] counts=new String[] {"500","1000","1500"};
        String[] counts=new String[] {"10","20","30"};
        List<CmdConsolePrintVo> cmdConsolePrintVoList=new ArrayList<>();
        for (int i = 0; i < counts.length; i++) {
            CmdConsolePrintVo cmdConsolePrintVo=abs.testWayByAbs(counts[i],concurrency,url);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cmdConsolePrintVoList.add(cmdConsolePrintVo);
        }


        List<AbsModel> absModelList=new ArrayList<>();
        for (int i = 0; i < cmdConsolePrintVoList.size(); i++) {
            CmdConsolePrintVo cmdConsolePrintVo=cmdConsolePrintVoList.get(i);
            logger.error("端口为：{}",cmdConsolePrintVo.getStrServerPort());
            AbsModel absModel = abs.interfaceJson(cmdConsolePrintVo);
            System.out.println(absModel);
            absModelList.add(absModel);
        }
        ChartsData chartsData = abs.interfaceEcharts(absModelList);
        ResponseData responseData=new ResponseData();
        responseData.setAbsModels(absModelList);
        responseData.setChartsData(chartsData);
        return responseData;
    }
}
